
const QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
const {
  baseUrl
} = require("../../../config/config")

Page({
  data: {
    latitude: null,
    longitude: null,
    locationName: null
  },
  async onShow () {
    const location = await this.getMylocation();
    const latitude = location.latitude;
    const longitude = location.longitude;
    const locationDetail = (await this.getMylocationName(latitude, longitude))
    console.log(locationDetail)
    // const locationName =locationDetail.res.result.address;
    let locationName = (await this.getMylocationName(latitude, longitude)).res.result.address;
    let jsonLocation = (await this.getMylocationName(latitude, longitude)).res.result;
    let newLocation = (await this.getMylocationName(latitude, longitude)).res.result
    jsonLocation = JSON.stringify(jsonLocation);


    this.setData({
      latitude,
      longitude,
      locationName,
      newLocation
    });
    wx.hideLoading()
  },
  getMylocation () {
    return new Promise((resolve, reject) => {
      wx.getLocation({
        type: "gcj02",
        success: res => {
          resolve(res)
        },
      })
    })
  },
  toMar () {
    wx.navigateTo({
      url: '/pages/mar/index/index',
    })
  },
  getMylocationName (latitude, longitude) {
    //  本接口提供由坐标到坐标所在位置的文字描述的转换，输入坐标返回地理位置信息和附近poi列表。
    const qqmapsdk = new QQMapWX({
      key: 'RO3BZ-6W7C6-ANUSK-E4LXZ-IEYL5-I7FOX'
    });
    return new Promise((resolve, reject) => {
      qqmapsdk.reverseGeocoder({
        location: { latitude, longitude },
        success: (res, data) => {
          resolve({ res, data })
        }
      })
    })
  },
  async pickImg () {
    if (this.data.locationName == '获取位置中..' || !this.data.locationName) {
      wx.showModal({
        title: '提示',
        content: '还没获取到位置'
      })
      throw new Error('还没获取到位置')
    };
    const fileName = await new Promise((resolve, reject) => {
      wx.chooseImage({
        count: 1,
        sizeType: ['compressed'],
        sourceType: ['camera'],
        success: (res) => {
          console.log(res);
          const tempFilePaths = res.tempFilePaths[0];
          console.log(tempFilePaths)
          wx.showLoading({
            title: '上传中',
          })
          wx.showNavigationBarLoading()
          wx.setNavigationBarTitle({
            title: '上传中'
          })
          wx.uploadFile({
            filePath: tempFilePaths,
            name: 'file',
            url: baseUrl + '/uploadfile',
            success: res => {
              wx.hideLoading()
              wx.hideNavigationBarLoading()
              wx.setNavigationBarTitle({
                title: ''
              })
              const fileName = JSON.parse(res.data).filename
              resolve(fileName)
            }
          })
        },
        fail: err => {
          wx.showModal({
            title: '提示',
            content: '你取消了，请重新拍照'
          })
          console.warn(err)
        }
      })
    });
    const latitude = this.data.latitude;
    const longitude = this.data.longitude;
    let jsonLocation = (await this.getMylocationName(latitude, longitude)).res.result;
    jsonLocation = JSON.stringify(jsonLocation);
    // const locationName = this.data.locationName;
    const locationName = jsonLocation;
    console.log(locationName, '管理员的签到')
    const data = await getApp().httpModule.daka({
      fileName,
      latitude,
      longitude,
      locationName
    });
    if (data.statusCode != 200) {
      wx.showModal({
        title: '提示',
        content: `${data.data.message}`
      })
    } else {
      wx.navigateTo({
        url: '/pages/list/index',
      })
    }
  },
  tolist () {
    wx.navigateTo({
      url: '/pages/list/index',
    })
  }
})